<?php

/**
 * Class for sending emails
 *
 * @author mina
 */
class MaxMailer
{

    const CODE_MESSAGE = 1;
    const CODE_REQUEST_NEW_PASSWORD = 2;
    /**
     *
     * @var sfController 
     */
    public $controller;

    /**
     * @var sfMailer 
     */
    public $mailer;

    public function __construct()
    {
        $this->controller = sfContext::getInstance()->getController();
        $this->mailer = sfContext::getInstance()->getMailer();
    }

    /**
     * @param int $emailCode code of the email in database
     * @param string $recieverEmail the email of the receiver
     * @param array $params array of parameters that should be displayed in the email
     * @author Mina Gerges
     */
    public function sendEMail($emailCode, $recieverEmail, $subject = null, $params = array())
    {
        //get the email template object by email code
        $emailObject = MailTemplateTable::getInstance()->findOneByCode($emailCode);
        if(!$subject)
        {
            $subject = $emailObject->getSubject();
        }
        //create instance from swiftMessage class and compose the message
        $message = MailSystem::newInstance();
        $message->setTo($recieverEmail);
        $message->setSubject($subject);

        //set the template of the email by the template name
        $message->setBodyFromTemplate($this->controller, $emailObject->getTemplate(), $params);
        return $this->mailer->send($message);
    }

    /**
     * @return MaxMailer
     */
    static function getInstance()
    {
        return new self();
    }

}

?>
